package com.kern.academic.repository;

import com.kern.academic.dto.RoleInfoDTO;
import com.kern.academic.entity.XRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * Created by caoshi at 7:29 2021-07-28
 */
public interface IRoleRepository extends JpaRepository<XRole, String>, JpaSpecificationExecutor<XRole> {


    /**
     *
     *  自定义结果实体类查询.
     *      1. new 后面跟上完整的类全路径
     *      2. RoleInfoDTO( ...) 括号里的字段是表对应实体的属性名
     *      3.  where ?1 is null or ro.id = ?1 当入参为null 时查询全部, 否则按参数值匹配
     * @param id
     * @return
     */
    @Query(value = "select new com.kern.academic.dto.RoleInfoDTO(ro.id, ro.roleName, ro.roleDesc, " +
            "ri.id, ri.name, ri.desc, ri.level, ri.code, ri.groupId, rg.name) " +
            "from XRole ro " +
            "left join XRoleRights rr on ro.id = rr.roleId " +
            "left join XRights ri on rr.rightsId = ri.id " +
            "left join XRightsGroup rg on ri.groupId = rg.id " +
            "where ?1 is null or ro.id = ?1")
    List<RoleInfoDTO> queryRoleInfo(String id);


}
